package club.pchome.demo;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;

import club.pchome.common.constant.SystemConstant;

public class Demo {
	static Logger logger = LoggerFactory.getLogger(Demo.class);
	private final static SimpleDateFormat FMT_FN = new SimpleDateFormat("HH:mm:ss");
	
 
	
	public static void main(String[] args) {
		
		System.out.println(new Demo().demo());
		
	}


	public String testBuy() {
		
		String userInfo = "{\"loginName\":\"13160833575\",\"pwd\":\"poiuytrew\"}";
		String JSESSIONID = AdmLogin(userInfo);
//		/EB588785435275165696
		String jsonData = "{\"coinId\":1007,\"entrustOrderNum\":\"EB588773674383835136\",\"securityCode\":\"poiuytrew\"}";
		String responseBody= JsoupToDataByCookieAndBody(SystemConstant.buy , jsonData , JSESSIONID);
		
		return responseBody;
	}
	

	/**
	 *  模拟测试 接口
	 */
	public String demo(){
		String userInfo = "{\"loginName\":\"18672685031\",\"pwd\":\"123456\"}";
		String JSESSIONID = AdmLogin(userInfo);

		String jsonData = "{\"pageNo\":1,\"pageSize\":50,\"coinId\":1008,\"entrustType\":1,\"payArea\":1}";
		String responseBody= JsoupToDataByCookieAndBody(SystemConstant.buyInList , jsonData , JSESSIONID);
		
		JSONObject account_data = JSONObject.parseObject(responseBody);
		
		String code = account_data.getString("code");
		if(code.equals("200")) {
			String dataList = account_data.getString("data");
			JSONObject list = JSONObject.parseObject(dataList);
			System.out.println(list);
			System.out.println("共有 " + list.getIntValue("totalCount") +" 个订单");
			List _list = list.getJSONArray("list");
			int num = 0;
			for (Object object : _list) {
				
				JSONObject __obj = (JSONObject) object;
				
				String userName = __obj.getString("userName");
//				System.out.println(userName);

				System.out.println("==============================================");
				int coinId = __obj.getIntValue("coinId");
				String coinIdName = null;
				if(coinId == 1001) { //USDT
					coinIdName = "USDT";
				}else if(coinId == 1002) { // 可售余额
					coinIdName = "可售余额";
				}else if(coinId == 1003) { // 可售额度
					coinIdName = "可售额度";
				}else if(coinId == 1004) { // 矿池钱包
					coinIdName = "矿池钱包";
				}else if(coinId == 1005) { // 矿池资产
					coinIdName = "矿池资产";
				}else if(coinId == 1006) { // 信用积分
					coinIdName = "信用积分";
				}else if(coinId == 1007) { // ADM
					coinIdName = "ADM";
				}else if(coinId == 1008) { // UT
					coinIdName = "UT";
				}else if(coinId == 1009) { // USDT冷钱包
					coinIdName = "USDT冷钱包";
				}

				System.out.println( coinIdName +": "+ coinId);

				System.out.println( "创建时间 : "+ FMT_FN.format(new Date(__obj.getIntValue("createTime"))));
				System.out.println( "修改时间 : "+ FMT_FN.format(new Date(__obj.getIntValue("modifyTime"))));
				System.out.println( "求购用户: "+userName);
				System.out.println( "求购级别: "+__obj.getIntValue("roleName"));
				System.out.println( "汇率: "+__obj.getIntValue("unitPrice"));
				System.out.println( "求购数量: "+__obj.getIntValue("entrustAmount"));
				System.out.println( "求购订单: "+__obj.getIntValue("entrustOrderId"));

				System.out.println( "目前的数量: " + __obj.getIntValue("currentVolume"));
				System.out.println( "委托状态: " + __obj.getIntValue("entrustStatus"));
				System.out.println( "委托类型: " + __obj.getIntValue("entrustType"));
				System.out.println( "不知道什么鬼子费率: " + __obj.getIntValue("rate"));

				System.out.println( "可售余额: "+__obj.getDoubleValue("ksyeAmount"));
				System.out.println( "可售额度: "+__obj.getDoubleValue("ksedAmount"));
				System.out.println( "订单识别码: "+__obj.getString("entrustOrderNum"));

			}
			
		}
		
		return null;
	}

	/**
	 * 登录ADM
	 * @param userInfo = "{\"loginName\":\"13113697731\",\"pwd\":\"poiuytrew\"}"
	 * @return
	 */
	public String AdmLogin(String userInfo) {
		Response res;
		String body = null;
		try {

			res = Jsoup.connect( SystemConstant.ADMLOGIN )
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
//					.proxy("127.0.0.1", 8888)
					.requestBody(userInfo)
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {
				//获取登录状态的cookie
				body = res.cookie("JSESSIONID");

			}else {
				logger.info("获取失败，重新获取!!!");
				body = "501";
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	/**
	 *  模拟测试
	 */
	public String ParsePageBody(){
		Response res;
		String body = null;
		try {
			
			res = Jsoup.connect( SystemConstant.BASEURL )
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
					.proxy("127.0.0.1", 8888)
					.requestBody("{\"loginName\":\"13076906186\",\"pwd\":\"poiuytrew\"}")
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {
				String cookieValue = res.cookie("JSESSIONID");
				System.out.println(new Date().getTime());
				
				
				
				body = new Demo().JsoupByHangList(cookieValue);
				
				System.out.println( body );
				
//				JSONObject jsonObject = JSON.parseObject(body);
//				
//				JSONArray dataList = jsonObject.getJSONArray("data");
//				
//				for (Object obj : dataList) {
//					
//					JSONObject __obj = (JSONObject) obj;
//					
//					Integer  coinId = __obj.getInteger("coinId");
//					String   totalAmount = __obj.getString("totalAmount");
//					String 	 coinName = __obj.getString("coinName");
//					
//					System.out.println(coinId + " _ " + totalAmount + " _ " + coinName);
//				}

				
			}else {
				logger.info("获取失败，重新获取!!!");
				body = "501";
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	
	/**
	 * 待处理任务数量
	 * @param cookie
	 * @return
	 */
	public String WaitTaskCount(String cookie) {
		Response res;
		String body = null;
		try {
			res = Jsoup.connect( SystemConstant.WaitTaskCount )
					.header("Host", "112.74.62.231")
					.header("Connection", "keep-alive")
					.header("Content-Length", "42")
					.header("Accept", "application/json, text/plain, */*")
					.header("Origin" , "https://112.74.62.231")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("Cookie", "JSESSIONID="+cookie)
					.header("Referer", "https://112.74.62.231/member-h5/")
					.requestBody("{}")
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {

				body = res.body();

			}else {
				body = "501";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	/**
	 * 待处理任务数量详情
	 * @param cookie
	 * @return
	 */
	public String WaitTaskCountDetail(String cookie) {
		Response res;
		String body = null;
		try {
			res = Jsoup.connect( SystemConstant.WaitTaskCountDetail )
					.header("Host", "112.74.62.231")
					.header("Connection", "keep-alive")
					.header("Content-Length", "42")
					.header("Accept", "application/json, text/plain, */*")
					.header("Origin" , "https://112.74.62.231")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("Cookie", "JSESSIONID="+cookie)
					.header("Referer", "https://112.74.62.231/member-h5/")
					.requestBody("{}")
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {

				body = res.body();

			}else {
				body = "501";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	/**
	 * 挂单
	 * @param url
	 * @param cookieValue
	 * @return
	 */
	public String JsoupByHangList(String cookie) {
		Response res;
		String body = null;
		try {
			res = Jsoup.connect( SystemConstant.hangList )
					.header("Host", "112.74.62.231")
					.header("Connection", "keep-alive")
					.header("Content-Length", "42")
					.header("Accept", "application/json, text/plain, */*")
					.header("Origin" , "https://112.74.62.231")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("Cookie", "JSESSIONID="+cookie)
					.header("Referer", "https://112.74.62.231/member-h5/")
					.requestBody("{\"pageNo\":1,\"pageSize\":50,\"entrustType\":1}")
					.timeout(6000)
//					.proxy("127.0.0.1", 8888)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {

				body = res.body();

			}else {
				body = "501";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	/**
	 * 签到
	 */
	public String openSign(String cookieValue) {
		Response res;
		String body = null;
		try {
			
			res = Jsoup.connect( SystemConstant.OPEN_SIGN )
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
					.header("cookie", "JSESSIONID="+cookieValue)
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {

				body = res.body();

			}else {
				logger.info("签到失败!!");
				body = "501";
			}


		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	

	/**
	 * @name 获取用户数据
	 * @param cookie
	 * @return
	 */
	public String getCapitalList(String cookieValue) {
		Response res;
		String body = null;
		try {
			
			res = Jsoup.connect( SystemConstant.ACCOUNT_DATA )
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
					.header("cookie", "JSESSIONID="+cookieValue)
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {

				body = res.body();

			}else {
				logger.error("获取用户信息失败!!");
				body = "501";
			}


		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	/**
	 * 验证是否可以签到
	 */
	public String checkSignStatus(String cookieValue) {
		Response res;
		String body = null;
		try {
			
			res = Jsoup.connect( SystemConstant.SIGN_STATUS )
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
//					.requestBody("{\"loginName\":\"13040861418\",\"pwd\":\"poiuytrew\"}")
					.header("cookie", "JSESSIONID="+cookieValue)
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {

				body = res.body();

			}else {
				logger.info("获取失败，重新获取!!!");
				body = "501";
			}


		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	/**
	 * @name 通用 post 取值方法
	 * @url 接口地址
	 * @param cookieValue
	 * @param requestBody
	 * @return
	 */
	public String JsoupToDataByCookieAndBody(String url ,String jsonToString, String cookieValue) {
		Response res;
		String body = null;
		try {
			res = Jsoup.connect( url )
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
					.header("cookie", "JSESSIONID="+cookieValue)
					.requestBody(jsonToString)
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {
				body = res.body();
			}else {
				body = "501";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	/**
	 * @name 通用 post 取值方法
	 * @url 接口地址
	 * @param cookieValue
	 * @return
	 */
	public String JsoupToDataByCookie(String url , String cookieValue) {
		Response res;
		String body = null;
		try {
			res = Jsoup.connect( url )
					.header("Accept-Encoding", "gzip, deflate, br")
					.header("Accept-Language", "zh-CN,zh;q=0.9")
					.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
					.header("Content-Type", "application/json;charset=utf-8")
					.header("Endpoint", "h5")
					.header("lang", "ZH")
					.header("cookie", "JSESSIONID="+cookieValue)
					.timeout(6000)
					.ignoreContentType(true)
					.validateTLSCertificates(false)
					.method(Method.POST)
					.execute();

			if(res.statusCode() == 200) {
				body = res.body();
			}else {
				body = "501";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	
	
	public static String httpGetHeader(String url,String cook,String header) throws IOException{
        //获取请求连接
        Connection con = Jsoup.connect(url);
        //请求头设置，特别是cookie设置
        con.header("Accept", "application/json, text/plain, */*");
        con.header("Accept-Encoding", "gzip, deflate, br");
        con.header("Accept-Language", "zh-CN,zh;q=0.9");
        con.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
        con.header("Content-Length", "44");
        con.header("Content-Type", "application/json;charset=utf-8");
        con.header("Endpoint", "h5");
		con.header("lang", "ZH");
		con.header("Referer", "https://112.74.62.231/member-h5/");
		con.header("Connection", "keep-alive");
		con.header("Host", "https://112.74.62.231");
        con.header("Cookie", cook);
        //发送请求
        Response resp=con.method(Method.POST).execute();        
        //获取cookie名称为__bsi的值
        String cookieValue = resp.cookie("__bsi");
        System.out.println("cookie  __bsi值：  "+cookieValue);
        //获取返回cookie所值
        Map<String,String> cookies = resp.cookies();
        System.out.println("所有cookie值：  "+cookies);
        //获取返回头文件值
        String headerValue = resp.header(header);
        System.out.println("头文件"+header+"的值："+headerValue);
        //获取所有头文件值
        Map<String,String> headersOne =resp.headers();
        System.out.println("所有头文件值："+headersOne);
        return headerValue; 
        
    }
}
